package com.mall.service.impl;

import com.mall.dao.AnnouncementDao;
import com.mall.domain.MsAnnouncement;
import com.mall.domain.Page;
import com.mall.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类（公告管理）
 * </p>
 *
 * @since 2023-12-19
 */
@Service
public class AnnouncementService {
    @Resource
    AnnouncementDao announcementDao;

    /**
     * 获取公告信息
     * @return
     */
    public List<MsAnnouncement> listMsAnnouncement(Page page) {
        //判断是否为模糊查询
        if(page.getQuery() == null){
            page.setQuery("%");
        } else {
            page.setQuery("%" + page.getQuery() + "%");
        }
        //设置分页查询开始值
        page.setNum((page.getNum() - 1) * page.getSize());
        List<MsAnnouncement> data = announcementDao.findAll(page);
        return data;
    }

    /**
     * 获取公告信息根据ID
     * @return
     */
    public MsAnnouncement listMsAnnouncementById(Integer id) {
        return announcementDao.findById(id);
    }

    public int findTotal(Page pageQuery) {
        Integer integer = announcementDao.findTotal(pageQuery.getQuery());
        return integer;
    }

    /**
     * 保存公告信息
     * @return
     */
    public void addMsAnnouncement(MsAnnouncement msAnnouncement) throws Exception {
        msAnnouncement.setDate(StringUtils.getNowDateTIme()); // 更新的时候插入当前时间
        announcementDao.addMsAnnouncement(msAnnouncement);
    }
    /**
     * 删除公告信息，根据ID
     * @return
     */
    public void delMsAnnouncement(Integer id) {
        announcementDao.del(id);
    }
    /**
     * 编辑公告信息，例如置顶。Id
     * @return
     */
    public void editMsAnnouncement(MsAnnouncement msAnnouncement) throws Exception {
        if(msAnnouncement.getId() == null) {
            new Exception("ID不能未空！");
        }
        msAnnouncement.setDate(StringUtils.getNowDateTIme());
        announcementDao.update(msAnnouncement);
    }
}
